package listbyorder.access401_500.test454;

import java.util.Arrays;

public class Solution1 {

    // 四数之和
    // 方法一 ： 优化,还是超时
    public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
        int len = A.length;
        int dlen = len * len;
        int count = 0;
        int[] temp1 = new int[dlen];
        int[] temp2 = new int[dlen];
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                temp1[i * len + j] = A[i] + B[j];
                temp2[i * len + j] = C[i] + D[j];
            }
        }
        Arrays.sort(temp1);
        Arrays.sort(temp2);
        int l = 0;
        int r = temp1.length - 1;
        while (l < temp1.length && r >= 0) {
            if (temp1[l] + temp1[r] == 0) {
                count++;
                l++;
                r--;
            } else if (temp1[l] + temp1[r] < 0) {
                l++;
            } else {
                r--;
            }
        }
        return count;
    }
}
